package com.appkubes.fintech.restful.application.user.controller;


import com.appkubes.fintech.common.response.Response;
import com.appkubes.base.core.entity.vo.UserKeyVO;
import com.appkubes.fintech.core.po.FintechUserBankCard;
import com.appkubes.fintech.restful.application.sms.service.SmsService;
import com.appkubes.fintech.restful.application.user.entity.dto.UserBankCardDTO;
import com.appkubes.fintech.restful.application.user.entity.vo.UserBankCardVO;
import com.appkubes.fintech.restful.application.user.service.UserBankCardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@Slf4j
@Api(tags = "用户银行卡信息")
@RestController
@RequestMapping("/user/bankcard")
public class UserBankCardController {

    @Resource
    UserBankCardService bankCardService;

    @Resource
    SmsService smsService;

    @ApiOperation("添加用户银行卡信息")
    @PostMapping("/add")
    public Response add(@RequestBody @Valid UserBankCardVO userBankCardVO,
                        BindingResult bindingResult) {
        log.info("begin add bankcard,userKey is {}",userBankCardVO.getUserKey());
        System.out.println("begin add bankcard,userKey is " + userBankCardVO.getUserKey());
        /*if (bindingResult.hasErrors()) {
            return Response.fail("参数检查失败");
        }*/

        //手机验证码检验
        boolean isValid = smsService.valid(userBankCardVO.getBankPhone(), userBankCardVO.getPhoneCode());
        if (!isValid) {
            System.out.println("急啊急啊急啊就");
            return Response.fail(isValid);
        }

        Integer effectRow = bankCardService.addBankCard(userBankCardVO);
        log.info("end add bankcard,userKey is {}",userBankCardVO.getUserKey());
        System.out.println("end add bankcard,userKey is " + userBankCardVO.getUserKey());
        return Response.success(effectRow);
    }

    @ApiOperation("查询银行信息")
    @GetMapping("/get")
    public Response<UserBankCardDTO> get(@Valid UserKeyVO userKeyVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return Response.fail("参数检查失败");
        }
        UserBankCardDTO bankCardDTO = new UserBankCardDTO();

        FintechUserBankCard bankCard = bankCardService.getBankCard(userKeyVO.getUserKey());
        if (bankCard != null) {
            BeanUtils.copyProperties(bankCard, bankCardDTO);
        }
        return Response.success(bankCardDTO);
    }


}
